<?php

namespace App\Http\Controllers\Customer;

use App\Helpers\Code;
use App\Models\Customer\Family;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Validator;

class FamilyController extends Controller
{

    /**
     * FamilyController constructor.
     *
     * @author lql
     */
    public function __construct()
    {
        $this->model = new Family();

        $this->_validators();
    }

    /**
     * 添加家系
     * @param Request $request
     * @return array
     *
     * @author lql
     */
    public function addFamily(Request $request)
    {
        try {
            $data = $request->only(['remark', 'state']);

            $validator = Validator::make($data, $this->rules, $this->messages);
            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $this->result['data'] = $this->model->addFamily($data);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 编辑家系
     * @return array
     *
     * @author lql
     */
    public function editFamily(Request $request, $id)
    {
        try {
            $data = $request->only(['remark', 'state']);

            $rules = [];
            foreach ($data as $key => $value) {
                switch ($key) {
                    default:
                        $rules[$key] = $this->rules[$key];
                }
            }

            $validator = Validator::make($data, $rules, $this->messages);
            if ($validator->fails()) {
                return $this->error($validator, Code::SYSTEM_VALIDATOR_ERROR);
            }

            $model = $this->model->editFamily($data, $id);
            $this->checked($model, [
                'null' => 'common.recored_not_found',
            ]);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取列表
     * @param Request $request
     * @return array
     *
     * @author lql
     */
    public function getFamilyList(Request $request)
    {
        try {
            $search = $this->handleSearch($request, $this->model);

            $limit = (int)$request->input('limit');

            $sorter = $this->handleSort($request, $this->model->sorters);

            $defaultSorter = $request->input('sorter') ?? 'desc';

            return preprocess($this->model->getFamilyList($limit, $sorter, $search, $defaultSorter));
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    /**
     * 获取指定家系成员
     * @param $id
     * @return array
     *
     * @author lql
     */
    public function getFamilyMemberAll($id)
    {
        try {
            $model = $this->model->getFamilyMemberAll($id);

            $this->checked($model, [
                'null' => 'common.recored_not_found',
            ]);

            return $this->result;
        } catch (\Exception $e) {
            return $this->throwError($e);
        }
    }

    public function _validators()
    {
        $this->rules = [
            'remark' => 'bail|nullable|max:200',
            'state' => 'bail|required|in:0,1',
        ];

        $this->messages = [
            'remark.max' => trans('customer.remark_max'),
            'state.required' => trans('customer.state_required'),
            'state.in' => trans('customer.state_in'),
        ];
    }
}
